package com.bishe.demo.modules.account.dao;

import com.bishe.demo.modules.account.entity.Role;
import com.bishe.demo.modules.account.entity.User;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.entity.Scenery;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserDao {
    @Insert("insert into account_user values(null,#{userName},#{password},#{userPhone},#{userEmail},#{userImage})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    void insertUser(User user);

    @Update("update account_user set password=#{password} where user_id=#{userId}")
    void updateUserPassword(User user);

    @Update("update account_user set user_phone=#{userPhone},user_email=#{userEmail},user_image=#{userImage} where user_id=#{userId}")
    void updateUserInfo(User user);

    @Delete("delete from account_user where user_id=#{userId}")
    void deleteUserByUserId(int userId);

    @Select("select * from account_user where user_id=#{userId}")
    @Results(id = "userResult", value = {
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_id", property = "role",
                    javaType = Role.class,
                    many = @Many(select = "com.bishe.demo.modules.account.dao.RoleDao.getRoleByUserId")),
            @Result(column = "user_id", property = "messages",
                    javaType = List.class,
                    many = @Many(select = "com.bishe.demo.modules.other.dao.MessageDao.getMessagesByUserId")),
            @Result(column = "user_id", property = "travels",
                    javaType = List.class,
                    many = @Many(select = "com.bishe.demo.modules.other.dao.TravelDao.getTravelsByUserId")),
            @Result(column = "user_id", property = "scenery",
                    javaType = Scenery.class,
                    many = @Many(select = "com.bishe.demo.modules.other.dao.SceneryDao.getSceneryByUserId"))
    })
    User getUserByUserId(int userId);

    @Select("select * from account_user where user_name=#{userName}")
    @ResultMap("userResult")
    User getUserByUserName(String userName);

    @Select("select * from account_user where user_name=#{userName} and password=#{password}")
    @ResultMap(value = "userResult")
    User getUserByUserNameAnsPassword(User user);

    @Select("select * from account_user au left join other_scenery_user osu on osu.user_id=au.user_id where osu.scenery_id=#{sceneryId}")
    User getUserBySceneryId(int sceneryId);

    @Select("select * from account_user au left join other_user_travel osu on osu.user_id=au.user_id where osu.travel_id=#{travelId}")
    User getUserByTravelId(int travelId);

    @Select("<script>" +
            "select * from account_user "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (user_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by user_id asc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<User> getUsersBySearchBeans(SearchBean searchBean);

    @Select("select user_name,user_phone,user_email from account_user as au right join tourism_route as tr on tr.fk_user_id = au.user_id " +
            "where tr.id = #{routeId}")
    User getUserByRouteId(Integer routeId);

    @Select("select user_name,user_phone,user_email,user_image from account_user au right join tourism_evaluation te " +
            "on au.user_id = te.fk_user_id where te.id = #{routeEvaluationId}")
    User getUserByRouteEvaluationId(Integer routeEvaluationId);

    @Select("select * from account_user au left join hotel_room_user hru on hru.user_id=au.user_id where hru.room_id=#{roomId}")
    User getUserByrRoomId(int roomId);

    @Select("select * from account_user au left join hotel_policy_user hpu on hpu.user_id=au.user_id where hpu.policy_id=#{policyId}")
    User getUserByPolicyId(int policyId);
}
